import { Injectable } from '@nestjs/common';
import { InjectEntityManager, InjectRepository } from '@nestjs/typeorm';
import UserCommemorationDayEntity from 'src/entities/user-commemoration-day.entity';
import { EntityManager,Repository } from 'typeorm';
import { HandleLoveDayDTO } from './love-day.dto';
import * as dayjs from 'dayjs'
import UserExtendEntity from 'src/entities/user-extend.entity';
import UserCommemorationDayModelEntity from 'src/entities/user-commemoration-day-model.entity';
import { computeCommemorationDay, computeSendSmsDate } from 'src/utils/loveDay';

@Injectable()
export class LoveDayService {
  constructor(
    @InjectRepository(UserCommemorationDayEntity)
    private readonly userCommemorationDayRepository: Repository<UserCommemorationDayEntity>,
    @InjectRepository(UserExtendEntity)
    private readonly userExtendEntityRepository: Repository<UserExtendEntity>,
    @InjectEntityManager() private entityManager: EntityManager,
  ){}

  async getLoveDayList(UserSysID:number){
    const resData={
      code:200,
      data:{},
      msg:''
    }
    const list = await this.userCommemorationDayRepository.find({
      select:['SysID','CommemorationName','CommemorationDate','LunarDate','IntervalDay','LunarOrSolar','Weekly','CountDown','RemindDate','RemindTime'],
      where:{
        Deleted:0,
        UserSysID:UserSysID
      },
      order:{
        SysID:'ASC'
      }
    })
    resData.data=list;
    return resData
  }
  
  async handleLoveDay(handleLoveDay:HandleLoveDayDTO){
    const {
      Type='add',SysID=-1,UserSysID=-1,CommemorationName='',BaseCommemorationDate='',
      LunarOrSolar=0,RemindDate=0,RemindTime='08:00',Circulation=0,CountDown=0
    }=handleLoveDay
    let resData={
      code:0,
      data:{},
      msg:''
    }
    switch (Type) {
      case 'add':
        //新增
        await this.entityManager.transaction(async entityManager=>{
          const now=dayjs().format('YYYY-MM-DD HH:mm:ss');
          const newCommemorationDate= computeCommemorationDay(BaseCommemorationDate,dayjs().format('YYYY-MM-DD'),Circulation,LunarOrSolar,CountDown)
          const userCommemorationDay=new UserCommemorationDayEntity();
          userCommemorationDay.BaseSysID=-1;
          userCommemorationDay.UserSysID=UserSysID;
          userCommemorationDay.CommemorationName=CommemorationName;
          userCommemorationDay.SingleCommemorationName=CommemorationName;
          userCommemorationDay.BaseCommemorationDate=BaseCommemorationDate;
          userCommemorationDay.CommemorationDate=newCommemorationDate.CommemorationDate;
          userCommemorationDay.LunarDate=newCommemorationDate.LunarDate;
          userCommemorationDay.LunarNumDate=newCommemorationDate.LunarNumDate;
          userCommemorationDay.SolarDate=newCommemorationDate.SolarDate;
          userCommemorationDay.LunarOrSolar=LunarOrSolar;
          userCommemorationDay.IntervalDay=newCommemorationDate.IntervalDay;
          userCommemorationDay.Weekly=newCommemorationDate.Weekly;
          userCommemorationDay.RemindDate=RemindDate;
          userCommemorationDay.RemindTime=RemindTime;
          userCommemorationDay.Circulation=Circulation;
          userCommemorationDay.Edited=1;
          userCommemorationDay.CountDown=CountDown;
          userCommemorationDay.UpdateDate='';
          userCommemorationDay.SendSmsIntervalDate=computeSendSmsDate(newCommemorationDate.CommemorationDate,CountDown,RemindDate);
          userCommemorationDay.CreateTime=now;
          userCommemorationDay.UpdateTime=now;
          userCommemorationDay.Deleted=0;
          
          await entityManager.save(UserCommemorationDayEntity,userCommemorationDay)
          await entityManager.update(UserExtendEntity,{
            UserSysID:UserSysID
          },
          {
            EditedCommemorationDay:1,
            UpdateTime:now
          })
          // throw new Error('主动抛出错误');
        })
          .then(()=>{
            resData={
              code:200,
              data:{
                
              },
              msg:'新增纪念日成功!'
            }
            return resData
          })
          .catch(e=>{
            resData={
              code:500,
              data:{
                e
              },
              msg:'数据处理有误'
            }
            return resData
          })
        break;
      case 'edit':
        //编辑
        await this.entityManager.transaction(async entityManager=>{
          const now=dayjs().format('YYYY-MM-DD HH:mm:ss');
          const newCommemorationDate= computeCommemorationDay(BaseCommemorationDate,dayjs().format('YYYY-MM-DD'),Circulation,LunarOrSolar,CountDown)
          const userCommemorationDay=new UserCommemorationDayEntity();
          userCommemorationDay.BaseSysID=-1;
          userCommemorationDay.UserSysID=UserSysID;
          userCommemorationDay.CommemorationName=CommemorationName;
          userCommemorationDay.SingleCommemorationName=CommemorationName;
          userCommemorationDay.BaseCommemorationDate=BaseCommemorationDate;
          userCommemorationDay.CommemorationDate=newCommemorationDate.CommemorationDate;
          userCommemorationDay.LunarDate=newCommemorationDate.LunarDate;
          userCommemorationDay.LunarNumDate=newCommemorationDate.LunarNumDate;
          userCommemorationDay.SolarDate=newCommemorationDate.SolarDate;
          userCommemorationDay.LunarOrSolar=LunarOrSolar;
          userCommemorationDay.IntervalDay=newCommemorationDate.IntervalDay;
          userCommemorationDay.Weekly=newCommemorationDate.Weekly;
          userCommemorationDay.RemindDate=RemindDate;
          userCommemorationDay.RemindTime=RemindTime;
          userCommemorationDay.Circulation=Circulation;
          userCommemorationDay.Edited=1;
          userCommemorationDay.CountDown=CountDown;
          userCommemorationDay.UpdateDate='';
          userCommemorationDay.SendSmsIntervalDate=computeSendSmsDate(newCommemorationDate.CommemorationDate,CountDown,RemindDate);
          userCommemorationDay.UpdateTime=now;
          userCommemorationDay.Deleted=0;
          
          await entityManager.update(UserCommemorationDayEntity,{
            SysID:SysID
          },userCommemorationDay)
          await entityManager.update(UserExtendEntity,{
            UserSysID:UserSysID
          },
          {
            EditedCommemorationDay:1,
            UpdateTime:now
          })
          await entityManager.delete(UserCommemorationDayModelEntity,{
            SysID:SysID
          })
          //删除modal表中的数据,避免数据有误

        })
          .then(()=>{
            resData={
              code:200,
              data:{
                
              },
              msg:'修改纪念日成功!'
            }
            return resData
          })
          .catch(e=>{
            resData={
              code:500,
              data:{
                e
              },
              msg:'数据处理有误'
            }
            return resData
          })
        break;
      case 'delete':
        //删除
        if(UserSysID!==-1){
          await this.userCommemorationDayRepository.update(
            {
              SysID:SysID,
              UserSysID:UserSysID
            },
            {
              UpdateTime:dayjs(new Date()).format('YYYY-MM-DD HH:mm:ss'),
              Deleted:1
            }
          )
        }
        resData.msg='删除纪念日成功!'
        break;
      default:
        break;
    }

    return resData
  }
}

